package com.changgou.search.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //定义交换机名称
    public static final String GOODS_UP_EXCHANGE ="goods_up_exchange";
    public static final String GOODS_DOWN_EXCHANGE ="goods_down_exchange";

    //队列的名称
    public static final String AD_UPDATE_QUEUE ="ad_update_queue";

    public static final String SEARCH_AD_QUEUE ="search_ad_queue";

    public static final String SEARCH_DEL_QUEUE ="search_del_queue";


    @Bean //声明队列
    public Queue queue(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //声明队列
    @Bean(SEARCH_AD_QUEUE)
    public Queue getQueue(){
        return new Queue(SEARCH_AD_QUEUE);
    }

    //声明队列
    @Bean(SEARCH_DEL_QUEUE)
    public Queue delQueue(){
        return new Queue(SEARCH_DEL_QUEUE);
    }



    //声明交换机
    @Bean(GOODS_UP_EXCHANGE)
    public Exchange getExchange(){
        //广播模式  durable:是否持久化
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    //声明交换机
    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange downExchange(){
        //广播模式  durable:是否持久化
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }


    //队列与交换机的绑定
    @Bean
    public Binding getBinding(@Qualifier(SEARCH_AD_QUEUE) Queue queue,
                              @Qualifier(GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding downBinding(@Qualifier(SEARCH_DEL_QUEUE) Queue queue,
                              @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
